var exec = require("child_process").exec;
var querystring = require("querystring");
var fs = require("fs");
var formidable = require("formidable");


function start(response, request) {
	console.log("start");
	
	/*
	function sleep(milliSeconds) {
		var startTime = new Date().getTime();
		while (new Date().getTime() < startTime + milliSeconds);
	}

	sleep(10000);
	*/
	
	/*
	exec("ping www.baidu.com -n 10" , function(error, stdout, stderr) {
		response.writeHead(200, {"content-type": "text/plain"});
		response.write(stdout);
		response.end();
	});
	*/

	/*
	var body = '<html>'+
    '<head>'+
    '<meta http-equiv="Content-Type" content="text/html; '+
    'charset=UTF-8" />'+
    '</head>'+
    '<body>'+
    '<form action="/upload" method="post">'+
    '<textarea name="text" rows="20" cols="60"></textarea>'+
    '<input type="submit" value="Submit text" />'+
    '</form>'+
    '</body>'+
    '</html>';
	*/
	
	var body = '<html>'+
    '<head>'+
    '<meta http-equiv="Content-Type" '+
    'content="text/html; charset=UTF-8" />'+
    '</head>'+
    '<body>'+
    '<form action="/upload" enctype="multipart/form-data" '+
    'method="post">'+
    '<input type="file" name="upload">'+
    '<input type="submit" value="Upload file" />'+
    '</form>'+
    '</body>'+
    '</html>';


    response.writeHead(200, {"Content-Type": "text/html"});
    response.write(body);
    response.end();
}

function upload(response, request) {
	console.log("Request handler 'upload' was called.");
	
	var form = new formidable.IncomingForm();
	console.log("about to parse");
	form.parse(request, function(error, fields, files) {
		console.log("parsing done");
		fs.renameSync(files.upload.path, "C:\\test.png");
		response.writeHead(200, {"content-type": "text/html"});
		response.write("received image:<br/>");
		response.write("<img src='/show' />");
		response.end();
	});

	/*
	response.writeHead(200, {"Content-Type": "text/plain"});
	response.write("you are sent: " + querystring.parse(postData).text);
	response.end();
	*/
}


function show(response, postData) {
	console.log("show was called.");
	fs.readFile("C:\\test.png", "binary", function(error, file) {
		if(error) {
			response.writeHead(500, {"content-type": "text/plain"});
			response.write(error + "\n");
			response.end();
		} else {
			response.writeHead(200, {"content-type": "image/png"});
			response.write(file, "binary");
			response.end();
		}
	});
}


exports.start = start;
exports.upload = upload
exports.show = show;